package com.bieyang.anjia.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bieyang.anjia.R;
import com.bieyang.anjia.module.Device;
import com.bieyang.anjia.net.NetManager;
import com.bieyang.anjia.utils.Constants;
import com.bieyang.anjia.utils.SharedPrefsUtil;
import com.bieyang.anjia.utils.Show;
import com.bieyang.anjia.utils.SubUtils;

/**
 * 忠机设置
 * */
@SuppressLint("HandlerLeak")
public class AlarmSetActivity extends BaseNomalActivity {
	RelativeLayout subTimeRelativeLayout, subMeasureRelativeLayout,
			subVoiceRelativeLayout, subMethodsRelativeLayout,
			deviceNameRelativeLayout;
	Activity activity;
	TextView deviceName, lookAlarmRecord, anjiaUid, alarm_time, alarm_measure,
			alarm_voice, alarm_methods;
	Button btnDelDevice;
	String uuid, id, memberId;
	Intent intent;
	String name;
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constants.DEVICE_DEL_SUCESS:
				progressDialog.dismiss();
				Show.toast(con, R.string.del_device_ok);
				setResult(124);
				finish();
				break;
			case Constants.DEVICE_DEL_FAIL:
				progressDialog.dismiss();
				Show.toast(con, R.string.del_device_fail);
				break;
			case Constants.QUERY_SUB_SET_SUCCESS:
				if (null != progressDialog) {
					progressDialog.dismiss();
				}
				Device device = (Device) msg.obj;
				if (null != device) {
					subUtils.saveSubSet(device, con);
				}
				setStates();
				break;
			case Constants.QUERY_SUB_SET_FAILED:
				if (null != progressDialog) {
					progressDialog.dismiss();
				}
				setStates();
				Show.toast(con, R.string.down_load_set_failed);
				break;
			case 0:
				progressDialog.dismiss();
				Show.toast(con, R.string.save_device_ok);
				finish();
				break;
			case 1:
				progressDialog.dismiss();
				Show.toast(con, R.string.save_device_fail);
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 初始化设备模型
	 * 
	 * @param device
	 */
	private void initDevice(Device device) {
		device.setStartTWhite(con.getString(R.string.sub_mode_start_time));
		device.setEndTWhite(con.getString(R.string.sub_mode_stop_time));
		device.setStartTNight(con.getString(R.string.sub_night_start_time));
		device.setEndTNight(con.getString(R.string.sub_night_stop_time));
		device.setHumanR(true);
		device.setFlashlight(true);
		device.setWeekWhite("1,2,3,4,5,6,7");
		device.setLarmWhite("MICRO_V_S");
		device.setDeterrenceVWhite(getString(R.string.sub_states_null));
	}

	/**
	 * 设置初始状态
	 */
	private void setStates() {
		alarm_time.setText("白天:"
				+ SharedPrefsUtil.getStringValue(con,
						Constants.DAY_MODE_START_TIME,
						getString(R.string.sub_mode_start_time))
				+ "-"
				+ SharedPrefsUtil.getStringValue(con,
						Constants.DAY_MODE_STOP_TIME,
						getString(R.string.sub_mode_stop_time))
				+ "\n夜间:"
				+ SharedPrefsUtil.getStringValue(con,
						Constants.NIGHT_MODE_START_TIME,
						getString(R.string.sub_night_start_time))
				+ "-"
				+ SharedPrefsUtil.getStringValue(con,
						Constants.NIGHT_MODE_STOP_TIME,
						getString(R.string.sub_night_stop_time)));
		alarm_measure.setText(SharedPrefsUtil.getStringValue(con,
				Constants.ALARM_MEASURES, getString(R.string.sub_body_recg)));
		alarm_methods.setText(SharedPrefsUtil.getStringValue(con,
				Constants.AlARM_METHODS, getString(R.string.sub_take_video)));
		alarm_voice.setText(SharedPrefsUtil.getStringValue(con,
				Constants.AlARM_PLAY_VOICE,
				getString(R.string.sub_states_null)));
	}

	@Override
	void setContent() {
		activity = this;
		setContentView(R.layout.activity_anjia_set);
		if (null == progressDialog) {
			progressDialog = SubUtils.showProgress(con);
		} else {
			progressDialog.show();
		}
		uuid = getIntent().getStringExtra("uuid");
		id = getIntent().getStringExtra("id");
		name = getIntent().getStringExtra("name");
		memberId = SharedPrefsUtil.getStringValue(con, "memberId", "0");
		if (null != uuid && !"ONLINE".equals(NetManager.subOnLineMap.get(uuid))) {
			Show.toast(con, R.string.sub_off_line);
		}
		Log.d("uuid", uuid + ";id:" + id + ";memberId:" + memberId);
	}

	@Override
	void initView() {
		OnClick onClick = new OnClick();
		anjiaUid = (TextView) findViewById(R.id.anjiaUid);
		anjiaUid.setText(uuid);
		TextView topLineTitle = (TextView) findViewById(R.id.topLineTitle);
		topLineTitle.setText(getString(R.string.sub_title));
		deviceName = (TextView) findViewById(R.id.divice_name);
		deviceName.setText(name);
		lookAlarmRecord = (TextView) findViewById(R.id.tvRecord); // 查看记录
		alarm_time = (TextView) findViewById(R.id.alarm_time);
		alarm_measure = (TextView) findViewById(R.id.alarm_measure);
		alarm_voice = (TextView) findViewById(R.id.alarm_voice);
		alarm_methods = (TextView) findViewById(R.id.alarm_methods);
		deviceNameRelativeLayout = (RelativeLayout) findViewById(R.id.diviceNameRelativeLayout);
		subTimeRelativeLayout = (RelativeLayout) findViewById(R.id.subTimeRelativeLayout);
		subMeasureRelativeLayout = (RelativeLayout) findViewById(R.id.subMeasureRelativeLayout);
		subVoiceRelativeLayout = (RelativeLayout) findViewById(R.id.subVoiceRelativeLayout);
		subMethodsRelativeLayout = (RelativeLayout) findViewById(R.id.subMethodsRelativeLayout);

		btnDelDevice = (Button) findViewById(R.id.btnDelDevice);
		btnDelDevice.setOnClickListener(onClick);
		// saveDeviceSet = (Button) findViewById(R.id.saveDeviceSet);
		subTimeRelativeLayout.setOnClickListener(onClick);
		subMeasureRelativeLayout.setOnClickListener(onClick);
		subVoiceRelativeLayout.setOnClickListener(onClick);
		subMethodsRelativeLayout.setOnClickListener(onClick);
		deviceNameRelativeLayout.setOnClickListener(onClick);
		lookAlarmRecord.setOnClickListener(onClick);

	}

	@Override
	void initData() {
		Device device1 = new Device();
		initDevice(device1);
		subUtils.saveSubSet(device1, con);
		if (null != memberId && null != uuid) {
			Device device = new Device();
			device.setUuid(uuid);
			device.setMemberId(memberId);
			App.netManager.queryCollectionTerminal(device, handler);
		} else {
			if (null != progressDialog) {
				progressDialog.dismiss();
			}
		}
	}

	@Override
	void setListener() {
	}

	class OnClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			boolean isOnline = null != uuid
					&& "ONLINE".equals(NetManager.subOnLineMap.get(uuid));
			switch (v.getId()) {
			case R.id.subTimeRelativeLayout:
				if (!isOnline) {
					Show.toast(con, R.string.sub_off_line);
				} else {
					startSetActivity(AlarmTimeActivity.class, 1001);
				}
				break;
			case R.id.subMeasureRelativeLayout:
				if (!isOnline) {
					Show.toast(con, R.string.sub_off_line);
				} else {
					startSetActivity(AlarmMeasuresActivity.class, 1002);
				}
				break;
			case R.id.subVoiceRelativeLayout:
				if (!isOnline) {
					Show.toast(con, R.string.sub_off_line);
				} else {
					startSetActivity(AlarmVoiceActivity.class, 1003);
				}
				break;
			case R.id.subMethodsRelativeLayout:
				if (!isOnline) {
					Show.toast(con, R.string.sub_off_line);
				} else {
					startSetActivity(AlarmMethodsActivity.class, 1004);
				}
				break;
			case R.id.diviceNameRelativeLayout:
				if (!isOnline) {
					Show.toast(con, R.string.sub_off_line);
				} else {
					startSetActivity(UpdateDiviceNameActivity.class, 1005);
				}
				break;
			case R.id.tvRecord:
				Intent intent = new Intent(con, AcAnXin.class);
				intent.putExtra("uuid", uuid);
				intent.putExtra("name", name);
				intent.putExtra("id", id);
				intent.putExtra("iSingle", true);
				startActivityForResult(intent, 1006);
				break;
			case R.id.btnDelDevice: // del device
				Show.dialog(con, con.getString(R.string.delete_sub),
						con.getString(R.string.delete_sub_confirm),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								deleteDevice();
							}
						});
				break;
			default:
				break;
			}

		}

	}

	/**
	 * 启动详细设置界面
	 * 
	 * @param cls
	 * @param requestCode
	 */
	private void startSetActivity(Class<?> cls, int requestCode) {
		Intent intent = new Intent(con, cls);
		intent.putExtra("uuid", uuid);
		intent.putExtra("name", name);
		intent.putExtra("id", id);
		startActivityForResult(intent, requestCode);
	}

	/**
	 * 删除忠机
	 */
	private void deleteDevice() {
		if (!App.isLogin) {
			Show.toast(con, R.string.please_login);
			return;
		}
		if (null == progressDialog) {
			progressDialog = SubUtils.showProgress(con);
		} else {
			progressDialog.show();
		}
		Device device = new Device();
		device.setDiviceId(id);
		App.netManager.deleteSub(device, handler);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		switch (resultCode) {
		case 2001:
			alarm_time.setText("白天:"
					+ SharedPrefsUtil.getStringValue(con,
							Constants.DAY_MODE_START_TIME,
							getString(R.string.sub_mode_start_time))
					+ "-"
					+ SharedPrefsUtil.getStringValue(con,
							Constants.DAY_MODE_STOP_TIME,
							getString(R.string.sub_mode_stop_time))
					+ "\n夜间:"
					+ SharedPrefsUtil.getStringValue(con,
							Constants.NIGHT_MODE_START_TIME,
							getString(R.string.sub_night_start_time))
					+ "-"
					+ SharedPrefsUtil.getStringValue(con,
							Constants.NIGHT_MODE_STOP_TIME,
							getString(R.string.sub_night_stop_time)));
			break;
		case 2002:
			alarm_measure.setText(SharedPrefsUtil
					.getStringValue(con, Constants.ALARM_MEASURES,
							getString(R.string.sub_body_recg)));
			break;
		case 2003:
			alarm_voice.setText(SharedPrefsUtil.getStringValue(con,
					Constants.AlARM_PLAY_VOICE,
					getString(R.string.sub_states_null)));
			break;
		case 2004:
			name = data.getStringExtra(Constants.DIVICE_NAME);
			deviceName.setText(name);
			break;
		case 2005:
			alarm_methods.setText(SharedPrefsUtil
					.getStringValue(con, Constants.AlARM_METHODS,
							getString(R.string.sub_take_video)));
			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void saveStatus() {
		// TODO Auto-generated method stub
		super.saveStatus();
		finish();
	}
}
